<?php
defined( '_VALID_MOS' ) or die( '非法链接.' );
require_once('common.inc.php');
require_once(_INC_PATH.'database.php');
require_once(_INC_PATH.'page.inc.php');
$id = intval($_REQUEST['id']);
if($id <= 0){
	Location("/404.php");
}
$db = new database();
$sql = "select id from `tbl_corporation` where id='$id' and del_flg='0'";
if(!$db->loadResult($sql)){
	Location("/404.php");
}

$sql = "select tc.*,ts.* from `tbl_corporation` tc left join `tbl_statistic` ts on ts.cor_id=tc.id where tc.id='$id' limit 1";
$data = $db->loadRow($sql);
$imgPath = findImg($data['id']);
if($imgPath == ""){
	$imgPath = "/images/noimage.jpg";
}
$data['imgPath'] = $imgPath;
$smarty->assign("name",$data['name']);//title显示
$smarty->assign("description",mb_substr($data['description'], 0, 180, "utf-8"));//description显示
$smarty->assign("corInfo",$data);

$sql_count = "select count(*) as total from `tbl_estimate` where cor_id='$id' and show_flg='1' and del_flg='0'";
$total = $db->loadResult($sql_count);
$curPage = @intval($_REQUEST['p']);
if($curPage < 1) $curPage = 1;
$showNum = _FORVIEWNUM;
$pageLink = "/corporation/".$id."/comment_{{p}}.html";
$page = new PageClass($total,$curPage,$showNum,$pageLink,5);
$limitStart = $page->limitStart;
$smarty->assign("pageList",$page->pageList);

$sql = "select te.*,tu.id as user_id from `tbl_estimate` te left join `tbl_user` tu on tu.id=te.user_id where te.cor_id='$id' and te.show_flg='1' and te.del_flg='0' order by update_time DESC limit $limitStart,$showNum";
$db->setQuery($sql);
$est_data = $db->loadList();
foreach($est_data as $key=>$value)
{
	if($est_data[$key]['user_id'] && $est_data[$key]['user_id'] != _getcookie("id")){
		$est_data[$key]['showRes'] = 1;
	}
	if($value['user_name'] == _VISITOR){
		$est_data[$key]['faceImg'] = '/images/87face.gif';
	}elseif(findImg($value['user_id'],"face")){
		$est_data[$key]['faceImg'] = findImg($value['user_id'],"face");
	}else{
		$est_data[$key]['faceImg'] = '/images/noface.gif';
	}
	$est_data[$key]['create_time'] = substr($value['create_time'], 0, 16);
	$est_data[$key]['content'] = content_replace($value['content'],$value['id'],'est');
	$sql = "select * from `tbl_response` where show_flg='1' and estimate_id='".$value['id']."'";
	$db->setQuery($sql);
	$res = $db->loadList();
	$est_data[$key]['resRow'] = $res;
	if(!empty($res) && count($res) > 2){
		$est_data[$key]['resOver'] = '1';
	}
	if(!empty($res)){
		foreach($res as $res_key=>$res_value){
			if($res_value["res_user_id"] != _getcookie("id")){
				$est_data[$key]['resRow'][$res_key]["showRes"] = 1;
			}
			$est_data[$key]['resRow'][$res_key]['res_user'] = $res_value['res_name'] != "" ? $res_value['res_name'] : $res_value['res_user_id'];
			$est_data[$key]['resRow'][$res_key]['est_user'] = $res_value['user_name'] != "" ? $res_value['user_name'] : $res_value['user_id'];
			$est_data[$key]['resRow'][$res_key]['content'] = content_replace($res_value['content'],$res_value['id'],'res');
		}
	}
}

$smarty->assign("estimateRow",$est_data);

/*企业排行**************************/
$sql = "select tc.* from tbl_corporation tc left join tbl_statistic ts on ts.cor_id=tc.id where tc.del_flg='0' order by ts.visit_total desc limit 10";
$db->setQuery($sql);
$rank_rows = $db->loadList();
$smarty->assign("rank_rows",$rank_rows);
/*企业排行 end**************************/

/*推荐企业**************************/
$sql = "select * from tbl_recom where del_flg='0' and type='qytj' limit 10";
$db->setQuery($sql);
$recom_rows = $db->loadList();
$smarty->assign("recom_rows",$recom_rows);
/*推荐企业 end**************************/

$pageinfo = Fetch("comment.tpl");

$dir = _CORPORATION_PATH;
if(!is_dir($dir))cMkDir($dir);
$inner_dir = _CORPORATION_PATH.$id;
if(!is_dir($inner_dir))cMkDir($inner_dir);
$filename = $inner_dir."/comment_{$curPage}.html";
writeFile($pageinfo, $filename);

echo $pageinfo;
die;